package cn.edu.qzuie.artist.module.td.controller.admin.worksimage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.worksimage.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.worksimage.WorksImageDO;
import cn.edu.qzuie.artist.module.td.service.worksimage.WorksImageService;

@Tag(name = "管理后台 - 作品集")
@RestController
@RequestMapping("/td/works-image")
@Validated
public class WorksImageController {

    @Resource
    private WorksImageService worksImageService;

    @PostMapping("/create")
    @Operation(summary = "创建作品集")
    @PreAuthorize("@ss.hasPermission('td:works-image:create')")
    public CommonResult<Long> createWorksImage(@Valid @RequestBody WorksImageSaveReqVO createReqVO) {
        return success(worksImageService.createWorksImage(createReqVO));
    }
    @PostMapping("/creates")
    @Operation(summary = "创建作品集")
    @PreAuthorize("@ss.hasPermission('td:works-image:create')")
    public CommonResult<Long> createWorksImages(@Valid @RequestBody WorksImageSaveReqVO createReqVO , @RequestParam Long workId) {
        return success(worksImageService.createWorksImageWithWorkId(createReqVO, workId));
    }

    @PutMapping("/update")
    @Operation(summary = "更新作品集")
    @PreAuthorize("@ss.hasPermission('td:works-image:update')")
    public CommonResult<Boolean> updateWorksImage(@Valid @RequestBody WorksImageSaveReqVO updateReqVO) {
        worksImageService.updateWorksImage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除作品集")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:works-image:delete')")
    public CommonResult<Boolean> deleteWorksImage(@RequestParam("id") Long id) {
        worksImageService.deleteWorksImage(id);
        return success(true);
    }
    @GetMapping("/get")
    @Operation(summary = "获得作品集")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:works-image:query')")
    public CommonResult<WorksImageRespVO> getWorksImage(@RequestParam("id") Long id) {
        WorksImageDO worksImage = worksImageService.getWorksImage(id);
        return success(BeanUtils.toBean(worksImage, WorksImageRespVO.class));
    }
//    @GetMapping("/gets")
//    @Operation(summary = "获得作品集")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('td:works-image:query')")
//    public CommonResult<WorksImageRespVO> getWorksImages(@RequestParam("id") Long id) {
//        WorksImageRespVO worksImage = worksImageService.getWorksImage(id);
//        return success(worksImage);
//    }

    @GetMapping("/page")
    @Operation(summary = "获得作品集分页")
    @PreAuthorize("@ss.hasPermission('td:works-image:query')")
    public CommonResult<PageResult<WorksImageRespVO>> getWorksImagePage(@Valid WorksImagePageReqVO pageReqVO) {
        PageResult<WorksImageDO> pageResult = worksImageService.getWorksImagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorksImageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出作品集 Excel")
    @PreAuthorize("@ss.hasPermission('td:works-image:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorksImageExcel(@Valid WorksImagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorksImageDO> list = worksImageService.getWorksImagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "作品集.xls", "数据", WorksImageRespVO.class,
                        BeanUtils.toBean(list, WorksImageRespVO.class));
    }

}